package org.management.utils;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * 功能描述：日期工具类
 * @author jiangyan
 */
public class DateUtils {
	
    /**
     * 默认显示日期的格式
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIMEF_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 默认显示日期时间毫秒格式
     */
    public static final String MSEL_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    /**
     * 默认显示简体中文日期的格式
     */
    public static final String ZHCN_DATE_FORMAT = "yyyy年MM月dd日";
    /**
     * 默认显示简体中文日期时间的格式
     */
    public static final String ZHCN_TIME_FORMAT = "yyyy年MM月dd日HH时mm分ss秒";
    /**
     * 默认显示简体中文日期时间毫秒格式
     */
    public static final String ZHCN_MSEL_FORMAT = "yyyy年MM月dd日HH时mm分ss秒SSS毫秒";
    /**
     * 获取日期串格式
     */
    public static final String YEAR_MONTH_DATE = "yyyyMMdd";
    /**
     * 获取日期串格式
     */
    public static final String YEAR_MONTH= "yyyyMM";
    
    /**
     * 获取日期串格式
     */
    public static final String YEAR= "yyyy";
    
    private static final String MONTH = "MM";
    
    /**
     * 获取日期时间串格式
     */
    public static final String TIME_STR_FORMAT = "yyyyMMddHHmmss";
    /**
     * 获取日期时间毫秒串格式
     */
    public static final String MSEL_STR_FORMAT = "yyyyMMddHHmmssSSS";
    /**
     * 获取时间毫秒串格式
     */
    public static final String MSSSEL_STR_FORMAT = "HHmmssSSS";
    
    private static DateFormat dateFormat = null;
    private static DateFormat dateStrFormat = null;
    private static DateFormat dateTimeFormat = null;
    private static DateFormat zhcnDateFormat = null;
    private static DateFormat zhcnDateTimeFormat = null;
    
    static {
        dateFormat = new SimpleDateFormat(DATE_FORMAT);
        dateStrFormat = new SimpleDateFormat(YEAR_MONTH_DATE);
        dateTimeFormat = new SimpleDateFormat(TIMEF_FORMAT);
        zhcnDateFormat = new SimpleDateFormat(ZHCN_DATE_FORMAT);
        zhcnDateTimeFormat = new SimpleDateFormat(ZHCN_TIME_FORMAT);
    }
    
    public static Date parseDate(String dateStr){
    	try {
			return dateFormat.parse(dateStr);
		} catch (ParseException e) {
			return new Date();
		}
    }
    
    public static Date parseDateTime(String dateStr){
    	try {
			return dateTimeFormat.parse(dateStr);
		} catch (ParseException e) {
			return new Date();
		}
    }
    
    public static int getNowYear(){
    	Calendar calendar=Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		return year;
    }
    
    public static String getNowDateTime(){
    	return dateTimeFormat.format(new Date());
    }
    
    /**
     * 时间 -字符串
     * */
    public static String dateToString(Date date,String dateFormat){
    	SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);  
		
		return sdf.format(date);  
    }
    
    public static String formatIntToString (int cTime){
    	String ct=cTime+"";
    	String yy=ct.substring(0, 4);
    	String mm=ct.substring(4, 6);
    	String dd=ct.substring(6, 8);
    	return yy+"-"+mm+"-"+dd;
    }
    /**
     * 将yyyy-mm-dd格式的日期String转换为 yyyymmdd格式的 int
     * @param cTime
     * @return int
     */
   public static int formatStringToInt(String cTime){
	   String [] temp = cTime.split("-");
	   String temp2=temp[0]+temp[1]+temp[2];
	   int intTime = Integer.parseInt(temp2);
	   return intTime;
   }
    /**
     * 获取今天的日期，格式如：2006-11-09
     * @return String - 返回今天的日期
     */
    public static String getToday() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     * 获取今天的日期，格式自定
     * @param pattern - 设定显示格式
     * @return String - 返回今天的日期
     */
    public static String getToday(String pattern) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }
    /**
     * 保证日期格式 为 2013年01月02日的形式
     * */				 
    public static String formatToChineseDate(String date){
    	String[] str=date.split("年");
		String year=str[0];
		String[] str1=str[1].split("月");
		String month=str1[0].length()>1?str1[0]:"0"+str1[0];
	
		String dayString=str1[1].length()>3?str1[1]:"0"+str1[1];
		
		return year+"年"+month+"月"+dayString;
    }

    /**
     * 得到当前时间的前/后若干天的时间
     * 例如当前时间2006-05-16 间隔天数30天，则返回2006-04-16
     * @param days - 间隔天数
     * @return String - 返回当时的时间
     */
    public static String getInternalTimeByDay(int days) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        now.add(Calendar.DATE, days);
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到当前时间的前/后若干天的时间
     * 例如当前时间2006-05-16 间隔天数30天，则返回2006-04-16
     * @param days - 间隔天数
     * @param pattern - 设定显示格式
     * @return String - 根据显示格式返回当时的时间
     */
    public static String getInternalTimeByDay(int days, String pattern) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
        sdf.setTimeZone(TimeZone.getDefault());
        now.add(Calendar.DATE, days);
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到当前时间的前/后若干月的时间
     * 例如当前时间2006-05-16 间隔月数3月，则返回2006-02-16
     * @param months - 间隔月数
     * @return - 返回当时的时间
     */
    public static String getInternalTimeByMonth(int months) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        now.add(Calendar.MONTH, months);
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到当前时间的前/后若干月的时间
     * 例如当前时间2006-05-16 间隔月数3月，则返回2006-02-16
     * @param months - 间隔月数
     * @param pattern - 设定显示格式
     * @return - 根据显示格式返回当时的时间
     */
    public static String getInternalTimeByMonth(int months, String pattern) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
        sdf.setTimeZone(TimeZone.getDefault());
        now.add(Calendar.MONTH, months);
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到中文日期
     * @param dateStr - 日期串，格式为“yyyy-MM-dd”
     * @return String - 返回中文日期，格式为“yyyy年MM月dd日”
     */
    public static String chinaDate(String dateStr) {
        if (dateStr == null || "null".equals(dateStr) || "".equals(dateStr)) {
            return "";
        }
        java.util.Date d = getDate(dateStr, DATE_FORMAT);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(ZHCN_DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(d));
    }

    /**
     * 得到中文日期,自定设置格式
     * @param dateStr - 需要改变格式的时间串
     * @param inPattern - 时间串的格式
     * @param outPattern - 改为时间串的格式
     * @return String - 根据outPattern格式返回时间
     */
    public static String alterDateByDynamic(String dateStr, String inPattern, String outPattern) {
    	if (dateStr == null || "null".equals(dateStr) || "".equals(dateStr)) {
            return "";
        }
        java.util.Date d = getDate(dateStr, inPattern);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(outPattern);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(d));
    }

    /**
     * 比较当前日期和指定日期 return boolean
     * 如果当前日期在指定日期之后返回true否则返回flase
     * @param dateStr 指定日期
     * @param pattern 指定日期的格式
     * @return boolean
     */
    public static boolean dateCompare(String dateStr, String pattern) {
        boolean bea = false;
        SimpleDateFormat sd = new SimpleDateFormat(pattern);
        String isDate = sd.format(new java.util.Date());
        java.util.Date date1;
        java.util.Date date0;
        try {
            date1 = sd.parse(dateStr);
            date0 = sd.parse(isDate);
            if (date0.after(date1)) {
                bea = true;
            }
        } catch (ParseException e) {
            bea = false;
        }
        return bea;
    }

    /**
     * 比较指定两日期,如果dateStr1晚于dateStr2则return true;
     * @param dateStr1 指定日期
     * @param dateStr2 指定日期
     * @param pattern 指定日期的格式
     * @return boolean
     */
    public static boolean dateCompare(String dateStr1, String dateStr2, String pattern) {
        boolean bea = false;
        SimpleDateFormat sd = new SimpleDateFormat(pattern);
        java.util.Date date1;
        java.util.Date date0;
        try {
            date1 = sd.parse(dateStr1);
            date0 = sd.parse(dateStr2);
            if (date0.after(date1)) {
                bea = true;
            }
        } catch (ParseException e) {
            bea = false;
        }
        return bea;
    }

    /**
     * 设置间隔数后返回时间
     * @param type 间隔类型 秒或者天  秒的类型为s,天的类型为d
     * @param i 间隔数字 比如1秒或者一天
     * @return String 返回时间格式为“yyyy-MM-dd HH:mm:ss”
     */
    public static String dateAdd(String type, int i) {
        SimpleDateFormat df = new SimpleDateFormat(TIMEF_FORMAT);
        String str = getToday(TIMEF_FORMAT);
        // 当时的日期和时间
        Calendar c = Calendar.getInstance(); 
        if ("s".equals(type)) {
            int s = c.get(Calendar.SECOND);
            s = s + i;
            c.set(Calendar.SECOND, s);
            str = df.format(c.getTime());
            //分钟
        } else if ("min".equals(type)) {
        	int min=c.get(Calendar.MINUTE);
        	min=min+i;
        	c.set(Calendar.MINUTE, min);
        	str=df.format(c.getTime());
        } else if ("d".equals(type)) {
        	// 取出“日”数
            int d = c.get(Calendar.DAY_OF_MONTH); 
            d = d + i;
            // 将“日”数设置回去
            c.set(Calendar.DAY_OF_MONTH, d); 
            str = df.format(c.getTime());
        }
        return str;
    }

    /**
     * 得到当前日期，如"2001-03-16".
     *
     * @version 1.0
     * @author wanghaibo.
     */
    public static String curDate() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到当前详细日期、时间，如"2001-03-16 20:34:20".
     *
     */
    public static String curTime() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(TIMEF_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到当前详细日期、时间，如"2001-03-16 20:34:20".
     *
     * @version 1.0
     */
    public static String getTimeAfter(int n) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.add(Calendar.MINUTE, n);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(TIMEF_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到当前时间的前/后若干天的时间
     * @param days - 间隔时间
     * @return - 返回当时的时间
     * 例如当前时间2003-05-16 间隔天数30天，则返回2003-04-16
     */
    public static String getInternalTime(int days) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(TIMEF_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        now.add(Calendar.DATE, days);
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到当前时间的前/后若干天的时间
     * @param currentTime - 当前时间
     * @param iHour - 间隔时间
     * @return - 返回当时的时间
     * 例如当前时间2003-05-16 08:10:10 间隔时间3小时，则返回2003-05-16 05:10:10
     */
    public static String getTimeOut(String currentTime, int iHour) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date result = sdf.parse(currentTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(result);
            cal.add(Calendar.HOUR, iHour);
            currentTime = sdf.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return currentTime;
    }

    /**
     * 得到当前月底的前/后若干天的时间
     * @param days - 间隔时间
     * @return - 返回当时的时间
     */
    public static String getInternalTimeByLastDay(int days) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        int maxDay = now.getActualMaximum(Calendar.DAY_OF_MONTH);
        now.set(Calendar.DATE, maxDay);
        now.add(Calendar.DATE, days);
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到时间串
     * @param dateStr String 时间字符串
     * @param fmt String 时间格式
     * @return String 返回值
     */
    public static String getDateString(String dateStr, String fmt) {
        try {
            if (dateStr == null || dateStr.equals("")) {
                return "";
            }
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(fmt);
            //          java.text.DateFormat df = java.text.DateFormat.getDateTimeInstance();
            java.util.Date d = sdf.parse(dateStr);
            String newDate = sdf.format(d);
            return newDate;
        } catch (Exception e) {
            //log.debug("\n" + e.getMessage());
        }
        return "";
    }

    /**
     * 得到时间串
     * @param dateStr String 时间字符串
     * @return String 返回值
     */
    public static java.util.Date getDate(String dateStr) {
        try {
            if (dateStr == null || dateStr.equals("")) {
                return null;
            }
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(TIME_STR_FORMAT);
            java.util.Date d = sdf.parse(dateStr);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @return 得到当前时间目录例如 030524
     */
    public static String getCurrTimeDir() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(YEAR_MONTH_DATE);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     *
     * @return 得到上个月月份 如200505
     */
    public static String getYesterM() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.add(Calendar.MONTH, -2);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     *
     * @return 得到本年度年份 如2005
     */
    public static String getYear() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(YEAR);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     *
     * @return 得到本月月份 如09
     */
    public static String getMonth() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(MONTH);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     * 得到下一个月分，包括年，例如：
     * 2003－1 月份的上一个月份是2002－12
     * @param year
     * @param month
     * @return
     */
    public static String[] getBeforeMonth(String year, String month) {
        String[] time = new String[2];
        if ("12".equals(month)) {
            time[1] = "1";
            time[0] = String.valueOf(Integer.parseInt(year) + 1);
        } else {
            time[1] = String.valueOf(Integer.parseInt(month) + 1);
            time[0] = year;
        }
        return time;
    }

    /**
     * 得到上一个月
     * @param year 年
     * @param month 月
     * @return String[] 0为年,1为月
     * TODO
     */
    public static String[] beforeMonth(String year, String month) {
        String[] time = new String[2];
        if ("1".equals(month)) {
            time[1] = "12";
            time[0] = String.valueOf(Integer.parseInt(year) - 1);
        } else {
            time[1] = String.valueOf(Integer.parseInt(month) - 1);
            time[0] = year;
        }
        return time;
    }

    /**
     * 得到当前日期，按照页面日期控件格式，如"2001-3-16".
     * @return String
     * TODO
     */
    public static String curSingleNumDate() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        //String DATE_FORMAT = "yyyy-M-d";
        //String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        //   String DATE_FORMAT = "yyyyMMdd";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     * 取自当前日期后的第n天的日期
     * @param n int 之后n天
     * @return String
     */
    public static String getDateAfter(int n) {
        String time = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DATE);
            Calendar cal = Calendar.getInstance(TimeZone.getDefault());
            cal.add(Calendar.DAY_OF_MONTH, n);
            time = sdf.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return time;
    }

    /**
     * 得到半年前的日期
     * @return String
     */
    public static String getHalfYearBeforeStr() {
        java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
        /** @todo 取当前日期 */
        String month = "";
        int tMonth = cal.get(GregorianCalendar.MONTH) + 1;
        if (tMonth < 10) {
            month = "0" + tMonth;
        } else {
            month = "" + tMonth;
        }
        int tDay = cal.get(GregorianCalendar.DATE);
        String day = "";
        if (tDay < 10) {
            day = "0" + tDay;
        } else {
            day = "" + tDay;
        }
        //        String endDate = "" + cal.get(GregorianCalendar.YEAR) + month + day;
        /** @todo 取半年前日期 */
        cal.add(GregorianCalendar.MONTH, -6);
        tMonth = cal.get(GregorianCalendar.MONTH) + 1;
        if (tMonth < 10) {
            month = "0" + tMonth;
        } else {
            month = "" + tMonth;
        }
        tDay = cal.get(GregorianCalendar.DATE);
        day = "";
        if (tDay < 10) {
            day = "0" + tDay;
        } else {
            day = "" + tDay;
        }
        String beginDate = "" + cal.get(GregorianCalendar.YEAR) + month + day;
        return beginDate;
    }

    /**
     * 返回比当前日期晚几分钟的一个yyyy-MM-dd HH:mm:ss的日期串晚的分钟数可由输入参数minute控制
     * @param minute
     * @return  返回延迟N分钟后的时间串
     */
    public static String getCurrentNextMinute(int minute) {
        String chargeStartTime = "";
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(new java.util.Date());
            cal.add(Calendar.MINUTE, minute);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            chargeStartTime = sdf.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chargeStartTime;
    }

    /**
     * 得到当前分钟
     * @return int
     */
    public static int getCurMin() {
        java.util.Date date = new java.util.Date();
        java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat("mm");
        int currentTime = Integer.parseInt(simpleDateFormat.format(date));
        return currentTime;
    }

    /**
     *
     * @param formatStr
     * @return
     */
    private static DateFormat getDateFormat(String formatStr) {
        if (formatStr.equalsIgnoreCase(DATE_FORMAT)) {
            return dateFormat;
        } else if (formatStr.equalsIgnoreCase(YEAR_MONTH_DATE)) {
        	return dateStrFormat;
        } else if (formatStr.equalsIgnoreCase(TIMEF_FORMAT)) {
            return dateTimeFormat;
        } else if (formatStr.equalsIgnoreCase(ZHCN_DATE_FORMAT)) {
            return zhcnDateFormat;
        } else if (formatStr.equalsIgnoreCase(ZHCN_TIME_FORMAT)) {
            return zhcnDateTimeFormat;
        } else {
            return new SimpleDateFormat(formatStr);
        }
    }

    //	public static Date getDate(String dateTimeStr)
    //	{				
    //		return getDate(dateTimeStr,DATATIMEF_STR);
    //	}
    /**
     * 按照默认formatStr的格式，转化dateTimeStr为Date类型
     * dateTimeStr必须是formatStr的形式
     * @param dateTimeStr
     * @param formatStr
     * @return
     */
    public static Date getDate(String dateTimeStr, String formatStr) {
        try {
            if (dateTimeStr == null || dateTimeStr.equals("")) {
                return null;
            }
            DateFormat sdf = getDateFormat(formatStr);
            java.util.Date d = sdf.parse(dateTimeStr);
            return d;
        } catch (ParseException e) {
            //throw new LangException(e);
        }
        return null;
    }

    /**
     * 将Date转换成字符串“yyyy-mm-dd hh:mm:ss”的字符串
     * @param date	日期
     * @return String 字符串
     * TODO
     */
    public static String dateToDateString(Date date) {
        return dateToDateString(date, TIMEF_FORMAT);
    }

    /**
     * 将Date转换成formatStr格式的字符串
     * @param date
     * @param formatStr
     * @return
     */
    public static String dateToDateString(Date date, String formatStr) {
        DateFormat df = getDateFormat(formatStr);
        return df.format(date);
    }

    /**
     * 返回一个yyyy-MM-dd HH:mm:ss 形式的日期时间字符串中的HH:mm:ss
     * @param dateTime
     * @return
     */
    public static String getTimeString(String dateTime) {
        return getTimeString(dateTime, TIMEF_FORMAT);
    }

    /**
     * 返回一个formatStr格式的日期时间字符串中的HH:mm:ss
     * @param dateTime
     * @param formatStr
     * @return
     */
    public static String getTimeString(String dateTime, String formatStr) {
        Date d = getDate(dateTime, formatStr);
        String s = dateToDateString(d);
        return s.substring(TIMEF_FORMAT.indexOf('H'));
    }

    /**
     * 获取当前日期yyyyMMdd的形式
     * @return
     */
    public static String getCurDateYMD() {
    	return dateToDateString(new Date(), YEAR_MONTH_DATE);
    }
    
    /**
     * 功能描述：获取当前日期的yyyyMMddHHmmssSSS形式
     * <p>
     * @author 李斌
     * <p>
     * @date 2017年10月20日 上午11:02:17
     * 
     * @return
     */
    public static String getCurDateYMDHMSS() {
    	return dateToDateString(new Date(), MSEL_STR_FORMAT);
    }
    
    /**
     * 功能描述：获取当前日期的yyyy-MM-dd HH:mm:ss.SSS形式
     * <p>
     * @author 李斌
     * <p>
     * @date 2017年10月20日 上午11:02:17
     * 
     * @return
     */
    public static String getCurDateYMDHmsssLong() {
    	return dateToDateString(new Date(), MSEL_FORMAT);
    }
    
    /**
     * 功能描述：获取当前日期的HHmmssSSS形式
     * <p>
     * @author 李斌
     * <p>
     * @date 2017年10月20日 上午11:10:15
     * 
     * @return
     */
    public static String getCurDateHMSSSSS() {
    	return dateToDateString(new Date(), MSSSEL_STR_FORMAT);
    }
    
    /**
     * 获取当前日期yyyy-MM-dd的形式
     * @return
     */
    public static String getCurDate() {
        return dateToDateString(new Date(), DATE_FORMAT);
    }

    /**
     * 获取当前日期yyyy年MM月dd日的形式
     * @return
     */
    public static String getCurZhCNDate() {
        return dateToDateString(new Date(), ZHCN_DATE_FORMAT);
    }


    /**
     * 获取当前日期时间yyyy-MM-dd HH:mm:ss的形式
     * @return
     */
    public static String getCurDateTime() {
        return dateToDateString(new Date(), TIMEF_FORMAT);
    }

    /**
     * 获取当前日期时间yyyy年MM月dd日HH时mm分ss秒的形式
     * @return
     */
    public static String getCurZhCNDateTime() {
        return dateToDateString(new Date(), ZHCN_TIME_FORMAT);
    }

    /**
     * 获取日期d的days天后的一个Date
     * @param d
     * @param days
     * @return Date
     */
    public static Date getInternalDateByDay(Date d, int days) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        now.add(Calendar.DATE, days);
        return now.getTime();
    }

    /**
     * 获取日期d的months月后的一个Date
     * @param d
     * @param months
     * @return Date
     */
    public static Date getInternalDateByMon(Date d, int months) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        now.add(Calendar.MONTH, months);
        return now.getTime();
    }

    /**
     * 获取日期d的years年后的一个Date
     * @param d
     * @param years
     * @return Date
     */
    public static Date getInternalDateByYear(Date d, int years) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        now.add(Calendar.YEAR, years);
        return now.getTime();
    }

    /**
     * 获取日期d的sec秒后的一个Date
     * @param d
     * @param sec
     * @return Date
     */
    public static Date getInternalDateBySec(Date d, int sec) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        now.add(Calendar.SECOND, sec);
        return now.getTime();
    }

    /**
     * 获取日期d的min分后的一个Date
     * @param d
     * @param min
     * @return Date
     */
    public static Date getInternalDateByMin(Date d, int min) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        now.add(Calendar.MINUTE, min);
        return now.getTime();
    }

    /**
     * 获取日期d的hours小时后的一个Date
     * @param d
     * @param hours
     * @return Date
     */
    public static Date getInternalDateByHour(Date d, int hours) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        now.add(Calendar.HOUR_OF_DAY, hours);
        return now.getTime();
    }

    /**
     * 根据一个日期字符串，返回日期格式，目前支持4种
     * 如果都不是，则返回null
     * @param date
     * @return 返回日期格式，目前支持4种
     */
    public static String getFormateStr(String date) {
    	//"yyyyMMdd"
    	String patternStr = "[0-9]{4}[0-9]{1,2}[0-9]{1,2}"; 
    	//"yyyy-MM-dd"
        String patternStr1 = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}"; 
        //"yyyy-MM-dd HH:mm:ss";
        String patternStr2 = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}"; 
        //"yyyy年MM月dd日"
        String patternStr3 = "[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日"; 
        //yyyy年MM月dd日HH时mm分ss秒
        String patternStr4 = "[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日[0-9]{1,2}时[0-9]{1,2}分[0-9]{1,2}秒";
        Pattern p = Pattern.compile(patternStr);
        Matcher m = p.matcher(date);
        boolean b = m.matches();
        if (b) {
        	return YEAR_MONTH_DATE;
        }
        p = Pattern.compile(patternStr1);
        m = p.matcher(date);
        b = m.matches();
        if (b) {
            return DATE_FORMAT;
        }
        p = Pattern.compile(patternStr2);
        m = p.matcher(date);
        b = m.matches();
        if (b) {
            return TIMEF_FORMAT;
        }
        p = Pattern.compile(patternStr3);
        m = p.matcher(date);
        b = m.matches();
        if (b) {
            return ZHCN_DATE_FORMAT;
        }
        p = Pattern.compile(patternStr4);
        m = p.matcher(date);
        b = m.matches();
        if (b) {
            return ZHCN_TIME_FORMAT;
        }
        return null;
    }

    /**
     * 将一个"yyyy-MM-dd HH:mm:ss"字符串，转换成"yyyy年MM月dd日HH时mm分ss秒"的字符串
     * @param dateStr
     * @return
     */
    public static String getZhCNDateTime(String dateStr) {
        Date d = getDate(dateStr);
        return dateToDateString(d, ZHCN_TIME_FORMAT);
    }

    /**
     * 将一个"yyyy-MM-dd"字符串，转换成"yyyy年MM月dd日"的字符串
     * @param dateStr
     * @return
     */
    public static String getZhCNDate(String dateStr) {
        Date d = getDate(dateStr, DATE_FORMAT);
        return dateToDateString(d, ZHCN_DATE_FORMAT);
    }

    /**
     * 将dateStr从fmtFrom转换到fmtTo的格式
     * @param dateStr
     * @param fmtFrom
     * @param fmtTo
     * @return
     */
    public static String getDateStr(String dateStr, String fmtFrom, String fmtTo) {
        Date d = getDate(dateStr, fmtFrom);
        return dateToDateString(d, fmtTo);
    }

    /**
     * 将小时数换算成返回以毫秒为单位的时间
     * @param hours
     * @return
     */
    public static long getMicroSec(BigDecimal hours) {
        BigDecimal bd;
        bd = hours.multiply(new BigDecimal(3600 * 1000));
        return bd.longValue();
    }

    /**
     * 获取Date中的分钟
     * @param d
     * @return
     */
    public static int getMin(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.MINUTE);
    }

    /**
     * 获取xxxx-xx-xx的日
     * @param d
     * @return
     */
    public static int getDay(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取月份，1-12月
     * @param d
     * @return
     */
    public static int getMonth(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取19xx,20xx形式的年
     * @param d
     * @return
     */
    public static int getYear(Date d) {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        now.setTime(d);
        return now.get(Calendar.YEAR);
    }

    /**
     * 得到d的上个月的年份+月份,如200505
     * @return
     */
    public static String getYearMonthOfLastMon(Date d) {
        Date newdate = getInternalDateByMon(d, -1);
        String year = String.valueOf(getYear(newdate));
        String month = String.valueOf(getMonth(newdate));
        return year + month;
    }

    /**
     * 得到当前日期的年和月如200509
     * @return String
     */
    public static String getCurYearMonth() {
        Calendar now = Calendar.getInstance(TimeZone.getDefault());
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(YEAR_MONTH);
        sdf.setTimeZone(TimeZone.getDefault());
        return (sdf.format(now.getTime()));
    }

    /**
     * @param year
     * @param month
     * @return
     */
    public static Date getNextMonth(String year, String month) {
        String datestr = year + "-" + month + "-01";
        Date date = getDate(datestr, DATE_FORMAT);
        return getInternalDateByMon(date, 1);
    }

    /**
     * @param year
     * @param month
     * @return
     */
    public static Date getLastMonth(String year, String month) {
        String datestr = year + "-" + month + "-01";
        Date date = getDate(datestr, DATE_FORMAT);
        return getInternalDateByMon(date, -1);
    }

    /**
     * 得到日期d，按照页面日期控件格式，如"2001-3-16"
     * @param d
     * @return
     */
    public static String getSingleNumDate(Date d) {
        return dateToDateString(d, DATE_FORMAT);
    }

    /**
     * 得到d半年前的日期,"yyyy-MM-dd"
     * @param d
     * @return
     */
    public static String getHalfYearBeforeStr(Date d) {
        return dateToDateString(getInternalDateByMon(d, -6), DATE_FORMAT);
    }

    /**
     * 判断当前时间是否为2号,是返回true，否返回false
     * @return
     */
	public static boolean isSecondDayOnCurrent() {
		Calendar cal = Calendar.getInstance();
		if (cal.get(Calendar.DATE) != 2) {
			return false;
		}
		return true;
	}
    
    /**
     * 得到当前日期D的月底的前/后若干天的时间,<0表示之前，>0表示之后
     * @param d
     * @param days
     * @return
     */
    public static String getInternalDateByLastDay(Date d, int days) {
        return dateToDateString(getInternalDateByDay(d, days), DATE_FORMAT);
    }
    
	/**
	 * 得到系统当前的分钟数,如当前时间是上午12:00,系统当前的分钟数就是12*60
	 * @return
	 */
	public static int getCurrentMinutes(){
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		
		sdf.setTimeZone(TimeZone.getDefault());
		String nowTime = sdf.format(now.getTime());
		
		String hours = nowTime.substring(8,10);
		String mins = nowTime.substring(10,12);
		
		int retInt = 0;
		
		try{
			retInt = Integer.parseInt(hours) * 24 + Integer.parseInt(mins);
		}catch(NumberFormatException numberFormateEx){
			
			numberFormateEx.printStackTrace();
		}
		
		return retInt;
	}
	
	 /**
     * 计算指定日期所属星期几
     * 0:周日
     * .....
     * 6:周六
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date){
    	int dayOfWeek = 0;
        Calendar cale = Calendar.getInstance(); 
        cale.setTime(date);
        dayOfWeek = cale.get(Calendar.DAY_OF_WEEK)-1; 
        return dayOfWeek;
    }
    
	/***
	 * 判断用户是否过期
	 * @param userDate
	 * @return
	 */
	public static boolean checkUserDate(int userDate){
		//当前时间日历(系统启动时间)
		Calendar currDate=Calendar.getInstance();
		Date date=new Date(System.currentTimeMillis());		
		currDate.setTime(date);
		//将传入的数据转化为日历格式
		String strUserDate = String.valueOf(userDate);
		Calendar userSetDate=Calendar.getInstance();
		userSetDate.set(Integer.parseInt(strUserDate.substring(0,4)),Integer.parseInt(strUserDate.substring(4, 6))-1,Integer.parseInt(strUserDate.substring(6, 8)),23,59,59);		
		if(currDate.before(userSetDate)){
			//未过期
			return true;
		}
		//过期
		return false;
	}
	
	/**
	 *  判断是否为闰年
	 * @param year
	 * @return
	 */
	public static boolean isLeapYear(int year) {
		boolean flag = false;
		// 闰年判断的条件:1.能被4整除,但不能被100整除;2.能被400整除
		if ((year % 4 == 0) && (year % 100 != 0)) {
			flag = true;
		} else if (year % 400 == 0) {
			flag = true;
		}
		return flag;
	}
	
	
	/**
	 * 	 以String类型返回当前的月份
	 * @return
	 */
	public static String getFirstDayMonth() {
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM");
		String nowdate = formater.format(new Date());
		return nowdate+"-01";
	}
	
    public static java.sql.Date getSqlDate(String dateTimeStr) {
        //DateUtils.getTIME_STR_FORMAT
        java.sql.Date d = new java.sql.Date(DateUtils.getDate(dateTimeStr, DateUtils.TIME_STR_FORMAT).getTime());
        //		d.setHours(Integer.parseInt(dateTimeStr.substring(8,10)));
        //		d.setMinutes(Integer.parseInt(dateTimeStr.substring(10,12)));
        //		d.setSeconds(Integer.parseInt(dateTimeStr.substring(12,14)));
        return d;
        
    }
    
    
    /**
     * 获取当前时间的前intervalMonth个月的第一天的整数值，如当前时间是20060708，则返回的时间为20060501
     * @param intervalMonth
     * @return
     */
	public static String getFwdMonthFirstDay(int intervalMonth) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -intervalMonth);
		cal.set(Calendar.DATE, 1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String fwdDayMonth = df.format(cal.getTime());

		return fwdDayMonth;
	}


	/**
	 *  判断当前时间是否为1号,是返回true，否返回false
	 * @return
	 */
	public static boolean isFirstDayOnCurrent() {
		Calendar cal = Calendar.getInstance();
		if (cal.get(Calendar.DATE) != 1) {
			return false;
		}
		return true;
	}

	/**
	 * add by mengxuefeng 返回当前月减去指定数后月份的最后一天日期
	 * 
	 * @param num
	 *            指定的被减数
	 */
	public static long getLastDayOfDate(int num) {

		Calendar cal = Calendar.getInstance();
		// 减去num后得到一个新的年月，
		cal.add(Calendar.MONTH, -num);
		// 得到新的年
		int year = cal.get(Calendar.YEAR);
		// 得到新的月，月可能是1位数或者2位数
		int month = cal.get(Calendar.MONTH) + 1;
		int day = 30;
		if (DateUtils.isLeapYear(year)) {
			day = 29;
		} else {
			day = 28;
		}
		return year * 10000 + month * 100 + day;
	}
	
	 /**
	  * 取得昨天的日期
	  * @return
	  */
	public static String getPrevDay() {
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.DAY_OF_MONTH, -1);
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
		String nowdate = formater.format(cale.getTime());
		return nowdate;

	}
	
	
	/**
	 * 获得本周一的日期  
	 */
    public static String getMondayOFWeek(){  
    	
         int mondayPlus = getMondayPlus();  
         GregorianCalendar currentDate = new GregorianCalendar();  
         currentDate.add(GregorianCalendar.DATE, mondayPlus);  
         Date monday = currentDate.getTime();  
           
         DateFormat df = DateFormat.getDateInstance();  
         String preMonday = df.format(monday);  
         return preMonday;  
    }  

    
    /**
     * 获得当前日期与本周日相差的天数  
     */
    private static int getMondayPlus() {  
        Calendar cd = Calendar.getInstance();  
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......  
        //因为按中国礼拜一作为第一天所以这里减1  
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         
        if (dayOfWeek == 1) {  
            return 0;  
        } else {  
            return 1 - dayOfWeek;  
        }  
    }
    
    public static String getNowData(){
    	SimpleDateFormat df = new SimpleDateFormat(TIMEF_FORMAT,Locale.CHINA);
		df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		return df.format(new Date());
    }
    
	/**获取指定月份的最后一天*/
	public static int getMonthLastDay(String year,String month){
		int dateOfMonth = 0;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,Integer.valueOf(year));
		//Java月份才0开始算
		cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
		dateOfMonth = cal.getActualMaximum(Calendar.DATE);
		return dateOfMonth;
	}
	
	/**
	 * 计算两个日期之间相差的天数  
	 * @param sdate
	 * @param edate
	 * @return
	 * @throws Exception
	 */
    public static int getDaysBetween(String sdate,String edate) throws Exception{  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(sdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(edate));    
        long time2 = cal.getTimeInMillis();         
        long betweenDays=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(betweenDays));     
    }
    
    /** 
	*字符串的日期格式的计算 
	*/
    public static int getDaysBetween(Date sdate,Date edate) throws Exception {    
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
    	sdate=sdf.parse(sdf.format(sdate));  
    	edate=sdf.parse(sdf.format(edate));  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(edate);    
        long time2 = cal.getTimeInMillis();         
        long betweenDays=(time2-time1)/(1000*3600*24);  
        return Integer.parseInt(String.valueOf(betweenDays));           
    }

    /**
     * 根据毫秒数获得其时间长短
     *
     * @param mss 要转换的毫秒数
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        StringBuilder result = new StringBuilder();
        if (days >= 1) {
            result.append(days).append(" 天 ");
        }
        if (hours >= 1) {
            result.append(hours).append(" 时 ");
        }
        if (minutes >= 1) {
            result.append(minutes).append(" 分 ");
        }
        if (seconds >= 1) {
            result.append(seconds).append(" 秒 ");
        }
        return result.toString();
    }
    /**
     *
     * @param begin 时间段的开始
     * @param end   时间段的结束
     * @return  输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
     * @author fy.zhang
     */
    public static String formatDuring(Date begin, Date end) {
        return formatDuring(end.getTime() - begin.getTime());
    }

    public static void main(String[] args) {
        System.out.println(formatDuring(new Long(246775)));
        System.out.println(DateUtils.dateAdd("min",5));
		
	}

	/**
     * 将Date转换成字符串“yyyy-mm-dd hh:mm:ss”的字符串
     * @param date	日期
     * @return String 字符串
     * TODO
     */
    public static String dateToDateStr(Date date) {
        return dateToDateString(date, DATE_FORMAT);
    }
	
}

